﻿using Max.PH5_2103A.Shopping.Frame.Domain.EFCore_DbFirst;
using Max.PH5_2103A.Shopping.Frame.DTO;
using Max.PH5_2103A.Shopping.Frame.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2103A.Shopping.Frame.API.Controllers
{
    /// <summary>
    /// 产品SKU控制器
    /// </summary>
    [Route("product")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        ISKUService _service;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public ProductController(ISKUService service)
        {
            _service = service;
        }

        /// <summary>
        /// SKU列表
        /// </summary>
        /// <param name="title">商品标题</param>
        /// <param name="categorId">分类id</param>
        /// <param name="brandId">品牌id</param>
        /// <param name="price1">价格区间1</param>
        /// <param name="price2">价格区间2</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="isAsc">升序还是降序</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页大小</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get(string? title = "",
            int categorId=-1,int brandId=-1,
            decimal price1 =-1,decimal price2 = -1,string sortField = "id",bool isAsc = true,
            int pageIndex = 1,int pageSize = 10)
        {
            ResponseResultDto response = new();

            pageIndex = pageIndex <= 0 ? 1 : pageIndex;
            pageSize = pageSize <= 0 ? 10 : pageSize;

            //title ??= "";  //当title==null时，取""，否则取title本身的值
            //Func<TbProductSku,bool> where = x=>x.Name.Contains(title);
            //var data = _service.GetAll(where,pageIndex,pageSize,out int total).Select(x => new {x.Name,x.InPrice,x.Id,x.SaleNumber,x.SalePrice });

            var data = _service.GetAll();  //获取所有数据

            #region 查询

            data = data.Where(x => x.Name.Contains(title ?? "")); //1、根据商品名称查询
            if (categorId >= 0)
            {
                data = data.Where(x => x.CategoryId == categorId);  //2、根据分类id去查找
            }
            if (brandId >= 0)
            {
                data = data.Where(x => x.BrandId == brandId);  //3、根据品牌id去查找
            }

            if (price1 > price2) //当第一个价格大于第二个价格的时候，把这两个价格进行交换
            {
                //如果不用第三个变量怎么实现
                price1 += price2;
                price2 = price1 - price2;
                price1 -= price2;
            }

            if(price1>=0 && price2>=0 && price1 <= price2)  //4、价格区间查询：第一个价格和第二价格一定是大于等于0的，并且第二个价格要大于第一个价格
            {
                data = data.Where(x => x.SalePrice >= price1 && x.SalePrice <= price2);
            }
            #endregion

            #region 排序
            if(sortField.ToLower().Equals("price")) //根据价格排序
            {
                if (isAsc) //价格的升序
                {
                    data = data.OrderBy(x => x.SalePrice);
                }
                else
                {
                    data = data.OrderByDescending(x => x.SalePrice);  //价格的降序
                }

            }else if(sortField.ToLower().Equals("sale")) //根据销量排序
            {
                if (isAsc) //销量的升序
                {
                    data = data.OrderBy(x => x.SaleNumber);
                }
                else
                {
                    data = data.OrderByDescending(x => x.SaleNumber);  //销量的降序
                }
            }
            else  //根据默认的id排序
            {
                if (isAsc) //id的升序
                {
                    data = data.OrderBy(x => x.Id);
                }
                else
                {
                    data = data.OrderByDescending(x => x.Id);  //id的降序
                }
            }
            #endregion

            int total = data.Count(); //符合条件的总记录数

            data = data.Skip((pageIndex-1) * pageSize).Take(pageSize);//分查询后的结果进行分页处理

            var d = data.Select(x => new { x.Name, x.InPrice, x.Id, x.SaleNumber, x.SalePrice });
            response.Data = new { items = d, total };
            return Ok(response);
        }

        /// <summary>
        /// 获取品牌、分类、商品列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("all")]
        public IActionResult Get()
        {
            ResponseResultDto response = new();

            response.Data = _service.LoadCategorBrandSKU();

            return Ok(response);
        }

        /// <summary>
        /// 获取明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public IActionResult GetDetail(string id)
        {
            ResponseResultDto response = new();

            var d = _service.GetSkuDetailById(id);

            response.Data = d;
            return Ok(response);
        }

    }
}
